package com.yq.service.impl;

import com.yq.mapper.AdminOrderMapper;
import com.yq.model.bo.ChangeOrderBO;
import com.yq.model.bo.OrdersByPageBO;
import com.yq.model.po.Orders;
import com.yq.model.po.State;
import com.yq.model.vo.OrderInfoVO;
import com.yq.model.vo.OrdersByPageVO;
import com.yq.model.vo.OrdersVO;
import com.yq.model.vo.orderinfovo.CurSpecEntity;
import com.yq.model.vo.orderinfovo.CurStateEntity;
import com.yq.model.vo.orderinfovo.SpecEntity;
import com.yq.service.AdminOrderService;
import com.yq.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;


public class AdminOrderServiceImpl implements AdminOrderService {


    @Override
    public OrdersByPageVO ordersByPage(OrdersByPageBO ordersByPageBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminOrderMapper mapper = sqlSession.getMapper(AdminOrderMapper.class);
        int count  = mapper.getTotalCount(ordersByPageBO);

        List<OrdersVO> ordersVO = mapper.ordersByPage(ordersByPageBO);
        OrdersByPageVO ordersByPageVO = new OrdersByPageVO();
        ordersByPageVO.setTotal(count);
        ordersByPageVO.setOrders(ordersVO);
        return ordersByPageVO;
    }

    @Override
    public void deleteOrder(int orderId) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminOrderMapper mapper = sqlSession.getMapper(AdminOrderMapper.class);
        try {
             mapper.deleteOrderById(orderId);
             sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public OrderInfoVO getOrderInfo(int orderId) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminOrderMapper mapper = sqlSession.getMapper(AdminOrderMapper.class);
        OrderInfoVO orderInfoVO = new OrderInfoVO();
        try {
            //分为几个部分
            //获取订单表的数据，
            Orders orders = mapper.getOrderInfo(orderId);
            // 获取规格表的数据，获取  也就是spec
            List<SpecEntity> list = mapper.getSpecInfo(orders.getGoodsid());
            //
            List<State> states = mapper.gteState();
            orderInfoVO.setAmount(orders.getAmount());

            CurSpecEntity curSpecEntity = new CurSpecEntity();
            curSpecEntity.setId(orders.getSpecid());

            orderInfoVO.setCurSpec(curSpecEntity);
            orderInfoVO.setNum(orders.getNumber());
            orderInfoVO.setGoodsDetailId(orders.getSpecid());
            orderInfoVO.setGoods(orders.getGoods());
            orderInfoVO.setId(orders.getGoodsid());
            orderInfoVO.setState(orders.getStateid());
            CurStateEntity curStateEntity = new CurStateEntity();
            curStateEntity.setId(orders.getStateid());
            orderInfoVO.setCurState(curStateEntity);
            orderInfoVO.setSpec(list);
            orderInfoVO.setStates(states);
            sqlSession.commit();
        }catch (Exception e){
            e.printStackTrace();
            sqlSession.rollback();

        }finally {
            sqlSession.close();
        }
        return orderInfoVO;
    }

    @Override
    public int changeOrder(ChangeOrderBO changeOrderBO) {
        SqlSession sqlSession = MybatisUtils.openSqlSession();
        AdminOrderMapper mapper = sqlSession.getMapper(AdminOrderMapper.class);
        int count=0;
        try {
            if(changeOrderBO.getState()==0){
                changeOrderBO.setStateInfo("未付款");
            }else if (changeOrderBO.getState()==1){
                changeOrderBO.setStateInfo("未发货");
            }else if (changeOrderBO.getState()==2){
                changeOrderBO.setStateInfo("已发货");
            }else if (changeOrderBO.getState()==3){
                changeOrderBO.setStateInfo("已到货");
            }
               count = mapper.changeOrder(changeOrderBO);
            //问题，就会我的state和stateid是直接存储到orders中的，所以处理的时候
            //就是一 先根据state的id找出statename，再进去修改orders的state
            // changeOrderBO 问题可不可以给changeOrderBO中加入一个人state的状态
            //经过测试可以
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
        }finally {
            sqlSession.close();
        }
        if (count==1){
            return 200;
        }
        return 404;
    }
}
